<template>
  <el-form :inline="true" class="demo-form-inline" v-model="form">
    <el-form-item label="一级分类" >
      <el-select
        placeholder="请选择"
        v-model="form.category1Id"
        @change="getCategory2List"
        :disabled="ischeck"
      >
        <el-option
          :label="attr.name"
          :value="attr.id"
          v-for="(attr, index) in category1List"
          :key="attr.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="二级分类">
      <el-select
        placeholder="请选择"
        v-model="form.category2Id"
        @change="getCategory3List"
        :disabled="ischeck"
      >
        <el-option
          :label="attr2.name"
          :value="attr2.id"
          v-for="(attr2, index) in category2List"
          :key="attr2.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="三级分类">
      <el-select
        placeholder="请选择"
        v-model="form.category3Id"
        @change="getAttrList"
        :disabled="ischeck"
      >
        <el-option
          :label="attr3.name"
          :value="attr3.id"
          v-for="(attr3, index) in category3List"
          :key="attr3.id"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: ["ischeck"],
  name: "CategorySelector",
  data() {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      form: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
    };
  },
  mounted() {
    /* 初始化获取一级列表 */
    this.getCategory1List();
  },
  methods: {
    /* 请求获取一级列表数据 */
    async getCategory1List() {
      try {
        const re = await this.$api.attr.getCategory1();
        if (re.code === 20000 || re.code === 200) {
          /* console.log(re); */
          this.category1List = re.data;
        } else {
          this.$message.error("获取一级列表数据失败");
        }
      } catch (error) {
        this.$message.error("请求获取一级列表数据失败");
      }
    },
    /* 选中一级列表 请求获取二级列表 */
    async getCategory2List() {
      this.$emit("getid");
      this.category2List = [];
      this.category3List = [];
      this.form.category2Id = "";
      this.form.category3Id = "";
      try {
        const re = await this.$api.attr.getCategory2(this.form.category1Id);
        if (re.code === 20000 || re.code === 200) {
          /* console.log(re); */
          this.category2List = re.data;
        } else {
          this.$message.error("获取二级列表数据失败");
        }
      } catch (error) {
        this.$message.error("请求获取二级列表数据失败");
      }
    },
    /* 选中二级列表 请求获取三级列表 */
    async getCategory3List() {
      this.$emit("getid");
      this.category3List = [];
      this.form.category3Id = "";
      try {
        const re = await this.$api.attr.getCategory3(this.form.category2Id);
        if (re.code === 20000 || re.code === 200) {
          /* console.log(re); */
          this.category3List = re.data;
        } else {
          this.$message.error("获取二级列表数据失败");
        }
      } catch (error) {
        this.$message.error("请求获取二级列表数据失败");
      }
    },
    /* 选中三级列表 将三级id传给父级 */
    getAttrList() {
      this.$emit("getid", this.form);
    },
  },
};
</script>

<style>
</style>